package com.guodaxia.gzh.config;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;
import java.net.Proxy;

/**
 * @author guojianquan
 * @date 2024/12/12 0:48
 */
//@Slf4j
@Configuration
public class GZHConfig {

    public static final Logger log = LoggerFactory.getLogger(GZHConfig.class);


    @Bean(name = "RedisTemplate")
    public RedisTemplate redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer(Object.class));

        redisTemplate.setHashKeySerializer(StringRedisSerializer.UTF_8);
        redisTemplate.setHashValueSerializer(new FastJsonRedisSerializer(Object.class));

        redisTemplate.setConnectionFactory(lettuceConnectionFactory);

        log.info("redisTemplate启动成功");
        return redisTemplate;
    }

    /*    maxTotal	连接池最大连接数	200-1000
                6
        defaultMaxPerRoute	单个域名最大并发连接数	50-200
                6
        connectTimeout	建立TCP连接的超时时间	5000ms
    3
        socketTimeout	等待服务器响应的超时时间	10000ms
    3
        connectionRequestTimeout	从连接池获取连接的等待时间	5000ms
    3*/
    @Bean
    public PoolingHttpClientConnectionManager connectionManager() {
        PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
        manager.setMaxTotal(200);          // 最大连接数:ml-citation{ref="6" data="citationList"}
        manager.setDefaultMaxPerRoute(50); // 单路由并发上限:ml-citation{ref="6" data="citationList"}
        return manager;
    }

    @Bean
    public CloseableHttpClient httpClient() {
        return HttpClientBuilder.create().setConnectionManager(connectionManager()).setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(5000)    // 连接超时(ms):ml-citation{ref="3" data="citationList"}
                .setSocketTimeout(10000)    // 数据读取超时(ms):ml-citation{ref="3" data="citationList"}
                .setConnectionRequestTimeout(5000) // 连接池获取超时(ms):ml-citation{ref="3" data="citationList"}
                .build()).build();
    }

    @Bean
    public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient());
        return factory;
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(clientHttpRequestFactory());
    }

    // 使用代理的RestTemplate 国泰网络
/*    @Bean(name = "proxy")
    public RestTemplate proxy() {
        RestTemplate restTemplate = new RestTemplate();
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("172.16.0.11", 3128)));
        restTemplate.setRequestFactory(requestFactory);
        return restTemplate;
    }*/
}
